﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.IO;

namespace CleanXMLFile
{
    class Worker
    {
        public ManualResetEvent resetEvent;
        private bool[] _lookup;

        public void CleanXML(object data)
        {
            WorkerArguments state = (WorkerArguments)data;
            String[] batch = state.batch;
            _lookup = state.LookupTable;

            FileStream fs = null;
            TextReader reader = null;
            StreamWriter writer = null;

            foreach (var f in batch)
            {
                try
                {
                    fs = Win32File.Open(f, FileMode.Open, FileAccess.ReadWrite);
                    reader = new StreamReader(fs);
                    string xmlText = reader.ReadToEnd();
                    string result = String.Empty;
                    
                    result = doFilter(xmlText);

                    //Ako result nije praza i ima manje znakova od ulaznog XMLa prepisi preko ulaznog xmla
                    if (!String.IsNullOrEmpty(result) && result.Length < xmlText.Length)
                    {
                        fs.SetLength(0);
                        fs.Flush();
                        writer = new StreamWriter(fs, System.Text.Encoding.UTF8);
                        writer.Write(result);
                        fs.Flush();

                        Console.WriteLine("File fixed: {0}", f);
                    }
                }
                catch (PathTooLongException e)
                {
                    Console.Write("Path to long for file: {0}\n", f);
                }
                catch (IOException e)
                {
                    Console.Write("IOException for file: {0}\n {1}\n", f, e.Message);
                }
                catch (Exception e)
                {
                    Console.Write("Exception for file: {0}\n {1}\n", f, e.Message);
                }
                finally
                {
                    if (writer != null)
                        writer.Close();
                    if (fs != null)
                        fs.Close();
                }
            }

            resetEvent.Set();
        }

        private String doFilter(String input)
        {
            char[] buffer = new char[input.Length];
            int index = 0;

            if (!String.IsNullOrEmpty(input))
            {    
                foreach (char c in input)
                {
                    if (_lookup[c])
                    {
                        buffer[index] = c;
                        index++;
                    }
                    else
                    {
                        Console.WriteLine("Illegal caracter found: {0}", c);
                    }
                }              
            }

            return new String(buffer,0,index);
        }
    }
}
